package com.aaron.kjviewutils;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.glide.GlideCircleTransform;
import com.bumptech.glide.Glide;

import org.taptwo.android.widget.ViewFlow;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：Aaron on 2017/2/13 17:05
 * 邮箱：kaijin.fang@edianniu.com
 */
public class DetailActivity extends BaseActivity {

    @Bind(R.id.detailToolbar)
    Toolbar detailToolbar;
    @Bind(R.id.leftBarIcon)
    ImageView leftBarIcon;
    @Bind(R.id.titleBg)
    ImageView titleBg;
    @Bind(R.id.airQualityLogo)
    ImageView airQualityLogo;
    @Bind(R.id.airQualityContent)
    TextView airQualityContent;
    @Bind(R.id.coldIndexLogo)
    ImageView coldIndexLogo;
    @Bind(R.id.coldIndexContent)
    TextView coldIndexContent;
    @Bind(R.id.dressingIndexLogo)
    ImageView dressingIndexLogo;
    @Bind(R.id.dressingIndexContent)
    TextView dressingIndexContent;
    @Bind(R.id.pollutionIndexLogo)
    ImageView pollutionIndexLogo;
    @Bind(R.id.pollutionIndexContent)
    TextView pollutionIndexContent;
    @Bind(R.id.washIndexLogo)
    ImageView washIndexLogo;
    @Bind(R.id.washIndexContent)
    TextView washIndexContent;
    @Bind(R.id.exerciseIndexLogo)
    ImageView exerciseIndexLogo;
    @Bind(R.id.exerciseIndexContent)
    TextView exerciseIndexContent;
    @Bind(R.id.weekDataViewFlow)
    ViewFlow weekDataViewFlow;

    private MainBean.MainSub wearths;

    @OnClick(R.id.leftBarIcon)
    void leftBarIconClick()
    {
        closeSelf(DetailActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.layout_detail);
        ButterKnife.bind(this);
        wearths = (MainBean.MainSub) this.getIntent().getExtras().getSerializable("data");
        initTitle();
        initAirDatas();
        initAirByDate();
    }

    private void initTitle()
    {
        detailToolbar.setTitle((null == wearths) ? "未知城市" : (TextUtils.isEmpty(wearths.city)) ? "未知城市" : wearths.city);
        setSupportActionBar(detailToolbar);
        loadBackground(leftBarIcon, resources.getDrawable(R.mipmap.back));
        detailToolbar.setNavigationIcon(null);
        Glide.with(DetailActivity.this).load("http://cyjctrip.qiniudn.com/1431/1346468437593p175vs43cjgoho0m14051h9n1ptj1f.jpg").animate(android.R.anim.slide_in_left).centerCrop().into(titleBg);
    }

    private void initAirDatas()
    {
        //空气质量
        Glide.with(DetailActivity.this).load("http://m2.app111.com/AppImg/160x160/2016/08/07/11408217061470551923708.jpg").transform(new GlideCircleTransform(DetailActivity.this)).dontAnimate().placeholder(R.mipmap.ic_launcher).into(airQualityLogo);
        airQualityContent.setText((null == wearths) ? "暂无空气质量数据" : (TextUtils.isEmpty(wearths.airCondition)) ? "暂无空气质量数据" : "空气质量："+wearths.airCondition);
        //感冒指数
        Glide.with(DetailActivity.this).load("http://img0.imgtn.bdimg.com/it/u=1333043833,3522232053&fm=15&gp=0.jpg").transform(new GlideCircleTransform(DetailActivity.this)).dontAnimate().placeholder(R.mipmap.ic_launcher).into(coldIndexLogo);
        coldIndexContent.setText((null == wearths) ? "暂无感冒指数数据" : (TextUtils.isEmpty(wearths.coldIndex)) ? "暂无感冒指数数据" : "感冒指数："+wearths.coldIndex);
        //穿衣指数
        Glide.with(DetailActivity.this).load("http://s7.rr.itc.cn/g/wapChange/20139_4_23/a8vegz9760966259340.jpg").transform(new GlideCircleTransform(DetailActivity.this)).dontAnimate().placeholder(R.mipmap.ic_launcher).into(dressingIndexLogo);
        dressingIndexContent.setText((null == wearths) ? "暂无穿衣指数数据" : (TextUtils.isEmpty(wearths.dressingIndex)) ? "暂无穿衣指数数据" : "穿衣指数："+wearths.dressingIndex);
        //空气质量指数
        Glide.with(DetailActivity.this).load("http://im1.tongbu.com/appicon/app/974/74/1048912974_42B13E01.jpg").transform(new GlideCircleTransform(DetailActivity.this)).dontAnimate().placeholder(R.mipmap.ic_launcher).into(pollutionIndexLogo);
        pollutionIndexContent.setText((null == wearths) ? "暂无空气质量指数数据" : (TextUtils.isEmpty(wearths.pollutionIndex)) ? "暂无空气质量指数数据" : "空气质量指数："+wearths.pollutionIndex);
        //洗车指数
        Glide.with(DetailActivity.this).load("http://img1.cache.netease.com/catchpic/C/CD/CD704FA16E6355C1CEF997775367CE87.jpg").transform(new GlideCircleTransform(DetailActivity.this)).dontAnimate().placeholder(R.mipmap.ic_launcher).into(washIndexLogo);
        washIndexContent.setText((null == wearths) ? "暂无洗车指数数据" : (TextUtils.isEmpty(wearths.washIndex)) ? "暂无洗车指数数据" : "洗车指数："+wearths.washIndex);
        //运动指数
        Glide.with(DetailActivity.this).load("http://src.onlinedown.net/images/h_iphone_imges/hotapp/10/logo/170123025849_66914.jpg").transform(new GlideCircleTransform(DetailActivity.this)).dontAnimate().placeholder(R.mipmap.ic_launcher).into(exerciseIndexLogo);
        exerciseIndexContent.setText((null == wearths) ? "暂无运动指数数据" : (TextUtils.isEmpty(wearths.exerciseIndex)) ? "暂无运动指数数据" : "运动指数："+wearths.exerciseIndex);
    }

    private void initAirByDate()
    {
        if(null!=wearths&&null!=wearths.future&&!wearths.future.isEmpty())
        {
            weekDataViewFlow.setAdapter(new WeekDataFilpperAdapter(DetailActivity.this, DetailActivity.this, mHandler,
                    wearths.future));
            weekDataViewFlow.setSelection(0);
        }
        else
        {

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            //关闭
            closeSelf(DetailActivity.this);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
